//Requires prototype, proto.menu,

///*Old Style*/Event.observe(window, 'load', init, false);
document.observe('dom:loaded', initUBoat);
window.onbeforeunload = promptForSave;

var activeLine=null;
var bLineNumbers=true;
var bSidebar = true;
var bInlineNotes = true;

var gbDirty = false;

function initUBoat() {
    Model_init();
    View_init();
    Controller_init();

    var anchor=$(window.location.hash.substring(1));
    if(anchor){
        activateLine(anchor);
    }
}

function deactivateLine()
{
    if(activeLine != null)
            activeLine.removeClassName('active');
}

function activateLine(element)
{
	activeLine = element;
    if(activeLine !=null){
            activeLine.addClassName('active');
            $('barStatus').update("Line "+ activeLine.identify());
            activeLine.scrollTo();
    }
}

function toggleLineNumbers()
{
    if(bLineNumbers == true)
    {
        $$("#paneSource ol").each(function(elmt) {
                elmt.addClassName('nolinenumbers')
        });
        bLineNumbers=false;
    }
    else
    {
        $$("#paneSource ol").each(function(elmt) {
                elmt.removeClassName('nolinenumbers')
        });
        bLineNumbers=true;
    }
}

function addBookmark(evt)
{
    var target = evt.target.next(0);
    var location = target.identify();
    //alert("Bookmark" + location);

    var addToLocation=$('listBookmarks');

    var bookmark = document.createElement('a');
    var item = document.createElement('li');

    var buttonClicker = document.createElement('img');
    buttonClicker.setAttribute('src', sUBoatHome + 'bookmark.png');
    buttonClicker.setAttribute('class', 'clicker'); // For IE :(

    bookmark.setAttribute('href', "#"+location);
    bookmark.addClassName('bookmark');
    bookmark.insert("Line "+ location + "\n");
    item.insert({top:bookmark});
    item.insert({top:buttonClicker});
    addToLocation.insert({top:item});

    var bookmarkID = bookmark.identify();
    EditInPlace.makeEditable( {
            type: 'text',
            id: bookmarkID,
            save_url: 'edit.php',
            is_wrapped:true
    } );
    flagDirty();
}

function addScrap()
{
    var addToLocation=$('listScraps');
    var buttonClicker = document.createElement('img');
    buttonClicker.setAttribute('src',  sUBoatHome + 'note.png');
    buttonClicker.setAttribute('class', 'clicker'); // For IE :(

    var scrap = document.createElement('span');
    var item = document.createElement('li');
    var newLine1 = document.createElement('br');
    var newLine2 = document.createElement('br');

    var text= $('textareaScrap').value;
    scrap.addClassName('scrap');
    scrap.insert(text);

    item.insert({top:scrap});
    item.insert({top:buttonClicker});
    item.insert({bottom:newLine1})
    item.insert({bottom:newLine2});
    addToLocation.insert({top:item});

    var scrapID = scrap.identify();
    EditInPlace.makeEditable( {
            type: 'textarea',
            id: scrapID,
            save_url: 'edit.php',
            is_wrapped:true
    } );
    flagDirty();
    $('textareaScrap').value = 'What\'s on your mind?';
    $('textareaScrap').addClassName('notedited');
}

function clearScrap()
{
    $('textareaScrap').value="";
}

function hideMenubar()
{
    $$('#barMenu ul li').each(function(elmt) {
            elmt.removeClassName('hover');
    });
}

function toggleSidebar()
{
    if(bSidebar==true){
        $('sideBarContents').hide();
        bSidebar=false;
    }
    else{
            $('sideBarContents').show();
            bSidebar=true;
    }
}

function toggleInlineNotes()
{
    if(bInlineNotes==true){
            $$('.ilnote').each(function (elmt){elmt.hide()});
            bInlineNotes=false;
    }
    else{
            $$('.ilnote').each(function (elmt){elmt.show()});
            bInlineNotes=true;
    }
}

function findTargetLine(evt)
{
    var looper = evt.target;

    while(!looper.hasClassName('sl')){
            looper = looper.up(0);
    }

    return looper;
}

function addInlineNote()
{
    var text= $('textareaScrap').value;
    attachNote(activeLine, text);
}

function hideNotes(evt)
{
    var target = evt.target.next(0);
    target.siblings().each(function (elmt) {if(elmt.hasClassName('ilnote')) elmt.hide();})
}

function showNotes(evt)
{
    var target = evt.target.next(0);
    target.siblings().each(function (elmt) {if(elmt.hasClassName('ilnote')) elmt.show();})
}

function gotoLine()
{
    var gotoLocation = $('SL' + $('inpLine').value);
    if(gotoLocation){
            $(gotoLocation).scrollTo();
            $('inpLine').value="";
            deactivateLine();
            activeLine = $(gotoLocation);
            activateLine();
    }
    $('inpLine').select();
}

function exportNotes()
{
    w=window.open();
    w.document.open();
    w.document.write("<h1>Notes</h1>");

    $$('.ilnote').each(function (elmt){w.document.write(elmt.innerHTML)});

    w.document.close();
}

function exportBookmarks()
{
    w=window.open();
    w.document.open();
    w.document.write("<h1>Bookmarks</h1>");

    $$('#listBookmarks li').each(function (elmt){w.document.write(elmt.innerHTML)});

    w.document.close();
}

function exportCode()
{
    w=window.open();
    w.document.open();
    w.document.write("<h1>Code</h1>");

    $$('.sl').each(function (elmt){w.document.write(elmt.innerHTML)});

    w.document.close();
}

function UBoat_saveFile()
{
    if(!isSaveAllowed()){
       return;
    }

    removeUIForSave(); //TODO: Better is to filter the view elements out
    hideMenubar(); // Otherwise menubar shows up in saved file
    var saveStatus = saveCurrentFile();
    if(saveStatus == 0){
        restoreUI();//TODO: Above
        flagClean(); //TODO: Check for success
        return;
    }
    alert(saveStatus);
}

function removeUIForSave()
{
    $$('.protomenu').each(function (elmt){elmt.remove();});
    $$('.view').each(function(elmt){elmt.remove();});
}

function restoreUI()
{
    View_restore();
    initMenus();
}

function flagDirty()
{
    if(gbDirty==false){
        $('SaveNotice').innerHTML = "*";
    }
    gbDirty = true;
}

function flagClean(){
    $('SaveNotice').innerHTML = "";
    gbDirty = false;
}

function UBoat_gotoSubmarine(){
    window.location = sNeptunerHome + 'submarine/' + sFilename + '.usub.html';
}

function UBoat_about() {
  alert('Neptuner UBoat \n (C) 2009-2010 The Oraclique \nDeveloped by sonofdelphi');
}

function UBoat_gotoNeptunerHome() {
  window.location = sNeptunerHome + "index.html";
}

function showAllHidden(){
    $$('.ilnote').each(
        function (elmt){
            if(!elmt.visible())
                elmt.show();
        }
    );
}

function removeBookmarks(){
    $$('#listBookmarks li').each(
        function (elmt){
            elmt.remove();
        }
    );
}

function removeScraps(){
    $$('#listScraps li').each(
        function (elmt){
            elmt.remove();
        }
    );
}

function promptForSave(){
    if(gbDirty)
        return "You have unsaved changes! Press Cancel to go back and save your changes.";
}

function removeNotes(evt){
    var target = evt.target.next(0);
    target.siblings().each(function (elmt) {if(elmt.hasClassName('ilnote')) elmt.remove();})
    target.removeClassName('hasNote');
    View_setClickerSource(target);
}

function isSaveAllowed()
{
    if(checkForEdits()){
        displayStatus("Please complete your edits before you save.");
        return false;
    }

    if(!(Prototype.Browser.Gecko | Prototype.Browser.IE)){
        displayStatus("Save not supported for your browser. Firefox-, Internet Explorer- only.");
        return false;
    }

    return true;
}

function checkForEdits()
{
    var isEditInProgress = $$('.editing');
    if(isEditInProgress.length > 0)
        return true;

    return false;
}

function displayStatus(msg){
    alert(msg);
}